package com.vv.vv_commonlibrary.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.vv.vv_commonlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 表格布局
 *
 * @author xia
 */
public class GirlLayout extends ViewGroup {


    private int columnCount = 3;
    private int rowItemWidth;

    private int verticalSpace = 0;
    private int horizontalSpace = 0;
    private float widthToHeightRatio;

    private List<Integer> rowHeights = new ArrayList<>();
    private List<List<View>> allView = new ArrayList<>();
    private boolean isOpenRatio;

    public GirlLayout(Context context) {
        this(context, null);
    }

    public GirlLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public void setColumnCount(int columnCount) {
        this.columnCount = columnCount;
    }

    private void initView(AttributeSet attrs) {
        final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.GirlLayout);
        columnCount = a.getInt(R.styleable.GirlLayout_columnCount, 3);
        isOpenRatio = a.getBoolean(R.styleable.GirlLayout_isOpenRatio, false);
        widthToHeightRatio = a.getFloat(R.styleable.GirlLayout_widthToHeightRatio, 1f);
        verticalSpace = a.getDimensionPixelSize(R.styleable.GirlLayout_verticalSpace, 3);
        horizontalSpace = a.getDimensionPixelSize(R.styleable.GirlLayout_horizontalSpace, 3);
        a.recycle();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int childCount = getChildCount();
        if (childCount <= 0) {
            setMeasuredDimension(widthMeasureSpec, getPaddingTop() + getPaddingBottom());
            return;
        }

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {

            if (isOpenRatio) {
                rowItemWidth = (width - (columnCount - 1) * verticalSpace - getPaddingLeft() - getPaddingRight()) / columnCount;
                int rowItemHeight = (int) (rowItemWidth * widthToHeightRatio);

                for (int i = 0; i < childCount; i++) {
                    View childView = getChildAt(i);
                    LayoutParams layoutParams = childView.getLayoutParams();
                    layoutParams.width = rowItemWidth;
                    layoutParams.height = rowItemHeight;
                    int childWidthSpec = getChildMeasureSpec(widthMeasureSpec, 0, rowItemWidth);
                    int childHeightSpec = getChildMeasureSpec(heightMeasureSpec, 0, rowItemHeight);
                    childView.measure(childWidthSpec, childHeightSpec);
                }

                int lineCount = childCount / columnCount;
                int rowCount = childCount % columnCount == 0 ? lineCount : lineCount + 1;

                int parentHeight = rowItemHeight * rowCount + (rowCount - 1) * horizontalSpace + getPaddingTop() + getPaddingBottom();
                setMeasuredDimension(width, parentHeight);

            } else {
                int parentHeight = 0;
                List<View> lineViews;
                allView.clear();
                lineViews = new ArrayList<>();

                int lineHeight = 0;
                for (int i = 0; i < childCount; i++) {
                    View childView = getChildAt(i);
                    measureChild(childView, widthMeasureSpec, heightMeasureSpec);
                    int childHeight = childView.getMeasuredHeight();

                    if (i != 0 && i % columnCount == 0) {
                        rowHeights.add(lineHeight);
                        parentHeight += lineHeight;
                        lineHeight = 0;
                        allView.add(lineViews);
                        lineViews = new ArrayList<>();
                    }

                    lineHeight = Math.max(lineHeight, childHeight);


                    lineViews.add(childView);
                }

                allView.add(lineViews);
                rowHeights.add(lineHeight);
                parentHeight += lineHeight;


                rowItemWidth = (width - (columnCount - 1) * verticalSpace - getPaddingLeft() - getPaddingRight()) / columnCount;

                int lineSize = allView.size();
                for (int i = 0; i < lineSize; i++) {
                    List<View> rowViews = allView.get(i);
                    int rowHeight = rowHeights.get(i);
                    for (View childView : rowViews) {
                        LayoutParams layoutParams = childView.getLayoutParams();
                        layoutParams.width = rowItemWidth;
                        layoutParams.height = rowHeight;

                        int childWidthSpec = getChildMeasureSpec(widthMeasureSpec, 0, rowItemWidth);
                        int childHeightSpec = getChildMeasureSpec(heightMeasureSpec, 0, rowHeight);
                        childView.measure(childWidthSpec, childHeightSpec);
                    }
                }
                parentHeight = parentHeight + (allView.size() - 1) * horizontalSpace + getPaddingTop() + getPaddingBottom();
                setMeasuredDimension(width, parentHeight);
            }
        } else {
            new Throwable("宽度必须为精确值");
        }

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        if (childCount <= 0) {
            return;
        }

        int rowTop = getPaddingTop();
        int rowBottom;
        if (isOpenRatio) {
            int left = getPaddingLeft();
            int rowItemHeight = (int) (rowItemWidth * widthToHeightRatio);
            rowBottom = rowTop + rowItemHeight;
            for (int i = 0; i < childCount; i++) {
                View childView = getChildAt(i);
                if (i != 0 && i % columnCount == 0) {
                    left = getPaddingLeft();
                    rowTop = rowBottom + horizontalSpace;
                    rowBottom = rowTop + rowItemHeight;
                }

                int right = left + rowItemWidth;
                childView.layout(left, rowTop, right, rowBottom);
                left = right + verticalSpace;
            }

        } else {
            int lineSize = allView.size();
            for (int i = 0; i < lineSize; i++) {
                List<View> rowViews = allView.get(i);
                rowBottom = rowTop + rowHeights.get(i);
                int rowSize = rowViews.size();
                int left = getPaddingLeft();
                int right;
                for (int rowIndex = 0; rowIndex < rowSize; rowIndex++) {
                    View childView = rowViews.get(rowIndex);
                    right = left + rowItemWidth;
                    childView.layout(left, rowTop, right, rowBottom);
                    left = right + verticalSpace;
                }
                rowTop = rowBottom + horizontalSpace;
            }
        }
    }

}
